﻿using System;
using System.Diagnostics;
using System.Drawing;
using DevExpress.XtraEditors;
using RedRiver.Framework.XtraCommon.Properties;

namespace RedRiver.Framework.XtraCommon.Templates{
    public class XtraCommonForm : XtraForm{
        private string _formTitle = string.Empty;
        private string _subTitle = " ";

        public XtraCommonForm(){
            InitializeComponent();
        }

        /// <summary>
        /// 窗体标题
        /// </summary>
        public string FormTitle{
            get { return _formTitle; }
            set{
                if (!_formTitle.Equals(value)) {
                    _formTitle = value;
                    if (!DesignMode){
                        Text = value;
                        TitleLabel.Text = _formTitle;
                    }
                }
            }
        }

        /// <summary>
        /// 副标题
        /// </summary>
        public string SubTitle{
            get { return _subTitle; }
            set{
                if (!_formTitle.Equals(value)){
                    _subTitle = value;
                    if (!DesignMode){
                        subTitleLabel.CustomizationFormText = subTitleLabel.Text = value;
                    }
                }
            }
        }

        private Image _titleImage;
        /// <summary>
        /// 标题图标
        /// </summary>
        public virtual Image TitleImage{
            get{
                if(_titleImage==null){
                    return Resources.Action_Open_Object_32x32;
                }
                return _titleImage;
            }
            set{if(value!=null){
                _titleImage = value;
            }}
        }

        #region Windows Form Designer generated code

        private DevExpress.XtraLayout.SimpleLabelItem TitleLabel;
        protected DevExpress.XtraLayout.LayoutControl TitleLayout;
        protected DevExpress.XtraLayout.LayoutControlGroup TitleLayoutGroup;
        protected DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem;
        private DevExpress.XtraLayout.SimpleLabelItem subTitleLabel;

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent(){
            this.TitleLayout = new DevExpress.XtraLayout.LayoutControl();
            this.TitleLayoutGroup = new DevExpress.XtraLayout.LayoutControlGroup();
            this.emptySpaceItem = new DevExpress.XtraLayout.EmptySpaceItem();
            this.TitleLabel = new DevExpress.XtraLayout.SimpleLabelItem();
            this.subTitleLabel = new DevExpress.XtraLayout.SimpleLabelItem();
            ((System.ComponentModel.ISupportInitialize)(this.TitleLayout)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.TitleLayoutGroup)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.TitleLabel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.subTitleLabel)).BeginInit();
            this.SuspendLayout();
            // 
            // TitleLayout
            // 
            this.TitleLayout.Dock = System.Windows.Forms.DockStyle.Top;
            this.TitleLayout.Location = new System.Drawing.Point(0, 0);
            this.TitleLayout.Name = "TitleLayout";
            this.TitleLayout.OptionsCustomizationForm.DesignTimeCustomizationFormPositionAndSize = new System.Drawing.Rectangle(432, 280, 252, 350);
            this.TitleLayout.Root = this.TitleLayoutGroup;
            this.TitleLayout.Size = new System.Drawing.Size(489, 55);
            this.TitleLayout.TabIndex = 0;
            this.TitleLayout.Text = "标题";
            // 
            // TitleLayoutGroup
            // 
            this.TitleLayoutGroup.AppearanceGroup.BackColor = System.Drawing.Color.LightSkyBlue;
            this.TitleLayoutGroup.AppearanceGroup.BackColor2 = System.Drawing.Color.White;
            this.TitleLayoutGroup.AppearanceGroup.GradientMode = System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal;
            this.TitleLayoutGroup.AppearanceGroup.Options.UseBackColor = true;
            this.TitleLayoutGroup.ContentImageAlignment = System.Drawing.ContentAlignment.MiddleRight;
            this.TitleLayoutGroup.CustomizationFormText = "标题";
            this.TitleLayoutGroup.EnableIndentsWithoutBorders = DevExpress.Utils.DefaultBoolean.True;
            this.TitleLayoutGroup.GroupBordersVisible = false;
            this.TitleLayoutGroup.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] {
            this.emptySpaceItem,
            this.TitleLabel,
            this.subTitleLabel});
            this.TitleLayoutGroup.Location = new System.Drawing.Point(0, 0);
            this.TitleLayoutGroup.Name = "Root";
            this.TitleLayoutGroup.OptionsCustomization.AllowDrag = DevExpress.XtraLayout.ItemDragDropMode.Disable;
            this.TitleLayoutGroup.Padding = new DevExpress.XtraLayout.Utils.Padding(5, 5, 5, 5);
            this.TitleLayoutGroup.Size = new System.Drawing.Size(489, 55);
            this.TitleLayoutGroup.Text = "标题";
            this.TitleLayoutGroup.TextVisible = false;
            // 
            // emptySpaceItem
            // 
            this.emptySpaceItem.AllowHotTrack = false;
            this.emptySpaceItem.CustomizationFormText = "emptySpaceItem";
            this.emptySpaceItem.Location = new System.Drawing.Point(201, 0);
            this.emptySpaceItem.MinSize = new System.Drawing.Size(24, 24);
            this.emptySpaceItem.Name = "emptySpaceItem";
            this.emptySpaceItem.Size = new System.Drawing.Size(278, 45);
            this.emptySpaceItem.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            this.emptySpaceItem.Text = "emptySpaceItem";
            this.emptySpaceItem.TextSize = new System.Drawing.Size(0, 0);
            // 
            // TitleLabel
            // 
            this.TitleLabel.AllowHotTrack = false;
            this.TitleLabel.AppearanceItemCaption.Font = new System.Drawing.Font("幼圆", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.TitleLabel.AppearanceItemCaption.Options.UseFont = true;
            this.TitleLabel.CustomizationFormText = "标题";
            this.TitleLabel.Location = new System.Drawing.Point(0, 0);
            this.TitleLabel.MaxSize = new System.Drawing.Size(201, 23);
            this.TitleLabel.MinSize = new System.Drawing.Size(201, 23);
            this.TitleLabel.Name = "TitleLabel";
            this.TitleLabel.Padding = new DevExpress.XtraLayout.Utils.Padding(10, 2, 5, 2);
            this.TitleLabel.Size = new System.Drawing.Size(201, 23);
            this.TitleLabel.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            this.TitleLabel.Text = "标题";
            this.TitleLabel.TextSize = new System.Drawing.Size(37, 16);
            // 
            // subTitleLabel
            // 
            this.subTitleLabel.AllowHotTrack = false;
            this.subTitleLabel.CustomizationFormText = "副标题";
            this.subTitleLabel.Location = new System.Drawing.Point(0, 23);
            this.subTitleLabel.MaxSize = new System.Drawing.Size(201, 22);
            this.subTitleLabel.MinSize = new System.Drawing.Size(201, 22);
            this.subTitleLabel.Name = "subTitleLabel";
            this.subTitleLabel.Size = new System.Drawing.Size(201, 22);
            this.subTitleLabel.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            this.subTitleLabel.Spacing = new DevExpress.XtraLayout.Utils.Padding(30, 0, 0, 0);
            this.subTitleLabel.Text = "副标题";
            this.subTitleLabel.TextSize = new System.Drawing.Size(37, 14);
            // 
            // XtraCommonForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(489, 367);
            this.Controls.Add(this.TitleLayout);
            this.Name = "XtraCommonForm";
            ((System.ComponentModel.ISupportInitialize)(this.TitleLayout)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.TitleLayoutGroup)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.TitleLabel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.subTitleLabel)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion Windows Form Designer generated code

        protected override void OnHandleCreated(EventArgs e){
            base.OnHandleCreated(e);
            if ((!DesignMode) && (TitleLayoutGroup.ContentImage == null)){
                //TitleLayoutGroup.ContentImage = TitleImage;
            }
        }

        /// <summary>
        /// 引发 System.Windows.Forms.Form.Load 事件。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e){
            if (!DesignMode){
                try{
                    if (TitleImage != null){
                        var image = (Bitmap) TitleImage;
                        TitleLayoutGroup.ContentImage = image;
                        Icon = Icon.FromHandle(image.GetHicon());
                    }
                } catch (Exception ex){
                    Trace.WriteLine(ex.ToString());
                }
            }
            if (string.IsNullOrWhiteSpace(FormTitle)){
                FormTitle = Text;
            }
            subTitleLabel.CustomizationFormText = subTitleLabel.Text = SubTitle;
            base.OnLoad(e);
        }
    }
}